package web;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import domain.PropertyInc;
import repository.PropertyIncDAO;

@WebServlet("/PropIncMod")
public class PropIncMod extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public PropIncMod() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		
		PropertyIncDAO allproDao = null;
		try {
			allproDao = new PropertyIncDAO();
		} catch (SQLException e) {
			e.printStackTrace();
		}

		HttpSession session = request.getSession();
		String ID= (String) session.getAttribute("ID");
		System.out.println(ID);
		ResultSet rs = allproDao.query(ID);	
		PropertyInc all = new PropertyInc();
		try {
			while(rs.next()){
				all.setID(rs.getString("物品ID"));
				all.setName(rs.getString("物品名称"));
				all.setPrice(rs.getString("物品价格"));
				all.setNumber(rs.getString("数量"));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		PropertyInc allprop = new PropertyInc();
		
		allprop.setID(all.getID());
		
		if(request.getParameter("name")=="") {
			allprop.setName(all.getName());
		}else {
			allprop.setName(request.getParameter("name"));
		}
		if(request.getParameter("price")=="") {
			allprop.setPrice(all.getPrice());
		}else {
			allprop.setPrice(request.getParameter("price"));
		}
		if(request.getParameter("number")=="") {
			allprop.setNumber(all.getNumber());
		}else {
			allprop.setNumber(request.getParameter("number"));
		}
		System.out.println(allprop.getID());
		System.out.println(allprop.getName());
		System.out.println(allprop.getPrice());
		System.out.println(allprop.getNumber());
		allproDao.update(allprop);
		request.getRequestDispatcher("PropertyIncDate.jsp").forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
